// pages/person/person.js
const app = getApp()
const wh = require("../../utils/wh.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    auth_modal: false, // 授权窗口
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    username: '这里是昵称', // 用户信息
    avatar: '../../images/head.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfoStr = wx.getStorageSync('W_Userinfo');
    if (userInfoStr != '' && userInfoStr != undefined) {
      let userInfo = JSON.parse(wx.getStorageSync('W_Userinfo'));
      if (userInfo != '') {
        this.setData({
          'username': userInfo.nickname,
          'avatar': userInfo.avatar
        })
      }
    }
  },
    // 取消授权
  cancel_auth: function () {
    wx.switchTab({
      url: '../index/index'
    });
    this.setData({
      auth_modal: false
    })
  },
  /**
   * 确认授权
   */
  bindGetUserInfo: function (e) {
    let userInfo = e.detail,
      that = this;
    this.setData({ userInfo: userInfo });
    if (e.detail.userInfo) {
      wx.showToast({
        title: '授权成功',
      })
      setTimeout(function () {
        let auth_rs = app.wlogin(userInfo);
        let employId = wx.getStorageSync('employId');
        if (!employId || employId == '') {
          app.employIdCallback = employId => {
            if (employId != '') {
              that.setData({
                auth_modal: false,
                username: userInfo.userInfo.nickName,
                avatar: userInfo.userInfo.avatarUrl
              })
            }
          }
        }
      }, 1500)
    } else {
      wx.switchTab({
        url: '../index/index'
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.app_auth(this)
  },
  //用户点击右上角分享
  onShareAppMessage: function () {
  }
})